package com.antd.auth.handler;

import com.antd.common.auth.security.AntdUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2020/11/19 10:33
 */
@Slf4j
@Component
public class AntdAuthenticationFailureEvenHandler implements ApplicationListener<AbstractAuthenticationFailureEvent> {
    @Override
    public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
        AuthenticationException authenticationException = event.getException();
        Authentication authentication = (Authentication) event.getSource();
        Object principal = authentication.getPrincipal();
        if (principal instanceof AntdUserDetails) {
            log.info("用户：{} 登录失败，异常：{}", ((AntdUserDetails) principal).getUsername(), authenticationException.getLocalizedMessage());
        }
    }
}
